package com.coollang.smartbra.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.beans.NewActionData;
import com.coollang.smartbra.constant.Constants;
import com.coollang.smartbra.constant.ResourceList;
import com.coollang.smartbra.dialog.ShareBitmapDialog;
import com.coollang.smartbra.interfaces.ActionType;
import com.coollang.smartbra.interfaces.ShareActionView;
import com.coollang.smartbra.presenter2.ShareActionPresenter;
import com.coollang.smartbra.utils.ActionTypeParse;
import com.coollang.smartbra.utils.ShareUtils;
import com.coollang.smartbra.utils.TimeUtils;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;
import com.umeng.socialize.UMShareAPI;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import eightbitlab.com.blurview.BlurView;
import eightbitlab.com.blurview.RenderScriptBlur;

/**
 * @author : Timmy
 */
public class NewShareActivity extends AbstractMvpActivity<ShareActionView, ShareActionPresenter> implements ShareActionView {

    @BindView(R.id.blurView)
    BlurView blurView;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_sport_finish)
    TextView tvSportFinish;
    @BindView(R.id.tv_hit)
    TextView tvHit;
    @BindView(R.id.share)
    TextView share;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.status_bar)
    View statusBar;
    @BindView(R.id.content)
    RelativeLayout content;
    @BindView(R.id.tv_finished)
    TextView tvFinished;
    private ShareUtils mShareUtils;
    private ShareBitmapDialog mShareBitmapDialog;
    private ArrayList<NewActionData> mActionDatas;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_new_share);
        ButterKnife.bind(this);
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusBar, this, content);

        initBlur();
        processIntent();
        initData();
    }

    @Override
    protected ShareActionPresenter createPresenter() {
        return new ShareActionPresenter();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    private void initBlur() {
        final float radius = 16f;
        final View decorView = getWindow().getDecorView();
        final View rootView = decorView.findViewById(android.R.id.content);
        final Drawable windowBackground = decorView.getBackground();
        blurView.setupWith(rootView)
                .windowBackground(windowBackground)
                .blurAlgorithm(new RenderScriptBlur(this, true))
                .blurRadius(radius);
    }

    private void processIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            mActionDatas = getIntent().getParcelableArrayListExtra(Constants.LIST);
            getPresenter().parseData(mActionDatas);
        }
    }

    private void initData() {
        int actionType = getPresenter().getActionType();
        String actionName = ResourceList.ACTION_NAME_LIST[actionType];
        int length = actionName.length();

        if (Constants.ACTION_NAME_ID1[actionType] == ActionType.PBZC) {
            setPbzcUI(getPresenter().getCorrectTime() * 1000);
        } else {
            setNumberUI(getPresenter().getCorrectCount(), length + 1);
        }

        getPresenter().saveActionData(mActionDatas);


        /**
         * 设置右边图片
         */
        int shareDrawable = ActionTypeParse.getShareDrawable(Constants.ACTION_NAME_ID1[actionType]);
        Drawable rightDrawable = ContextCompat.getDrawable(this, shareDrawable);
        rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
        tvSportFinish.setCompoundDrawables(null, null, rightDrawable, null);
    }

    private void setNumberUI(long num, int length) {
        String text = "本次共完成 " + num + " 个" + ResourceList.ACTION_NAME_LIST[getPresenter().getActionType()];
        SpannableString msp = new SpannableString(text);
        // 设置字体大小
        msp.setSpan(new RelativeSizeSpan(1.5f), 6, text.length() - length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        // 设置字体颜色
        msp.setSpan(new ForegroundColorSpan(0xFFFCC707), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new ForegroundColorSpan(0xFFF79444), 6, text.length() - length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tvFinished.setText(msp);
        setUMoney(num);
    }

    private void setPbzcUI(long num) {
        String time = TimeUtils.getFormatmillisecond(num);
        String text = "本次 " + ResourceList.ACTION_NAME_LIST[getPresenter().getActionType()] + "坚持了 " + time;
        SpannableString msp = new SpannableString(text);
        // 设置字体大小
        msp.setSpan(new RelativeSizeSpan(1.5f), text.length() - 8, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 设置字体颜色
        msp.setSpan(new ForegroundColorSpan(0xFFFCC707), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new ForegroundColorSpan(0xFFF79444), text.length() - 8, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tvFinished.setText(msp);

        setUMoney(num);
    }

    private void setUMoney(long num) {

        long uMoneyCount;
        int actionCode = Constants.ACTION_NAME_ID1[getPresenter().getActionType()];

        if (actionCode == ActionType.PBZC) {
            uMoneyCount = (int) (num / 1000) * ActionTypeParse.getUMoneyCount(actionCode);
        } else {
            uMoneyCount = num * ActionTypeParse.getUMoneyCount(actionCode);
        }

        String uMoney = "获得 " + uMoneyCount + " U币";
        SpannableString msp = new SpannableString(uMoney);

        // 设置字体大小
        msp.setSpan(new RelativeSizeSpan(1.5f), 2, uMoney.length() - 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        // 设置字体颜色
        msp.setSpan(new ForegroundColorSpan(0xFFFCC707), 0, uMoney.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new ForegroundColorSpan(0xFFF79444), 3, uMoney.length() - 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tvHit.setText(msp);
    }


    /**
     * ============================= 分享 =============================
     */
    private ShareBitmapDialog.OnclickListener mOnclickListener = new ShareBitmapDialog.OnclickListener() {
        @Override
        public void onCancel() {
            mShareBitmapDialog.dismiss();
        }

        @Override
        public void onWeixin() {
            mShareUtils.share(1);
        }

        @Override
        public void onWEXIN_CIRCLE() {
            mShareUtils.share(2);
        }

        @Override
        public void onWeibo() {
            mShareUtils.share(3);
        }

        @Override
        public void onQQ() {
            mShareUtils.share(4);
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }


    @OnClick({R.id.iv_close, R.id.share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finish();
                break;
            case R.id.share:
                if (mShareUtils == null) {
                    mShareUtils = new ShareUtils(this);
                    mShareUtils.initShare();
                    mShareBitmapDialog = new ShareBitmapDialog(this, null);
                    mShareBitmapDialog.setOnclickListener(mOnclickListener);
                }
                mShareBitmapDialog.show();
                break;
            default:
                break;
        }
    }
}
